
 
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:dio/dio.dart';

class DioPage extends StatefulWidget {
  DioPage({Key key}) : super(key: key);

  @override
  _DioPageState createState() => _DioPageState();
}

class _DioPageState extends State<DioPage> {

  List _List = [];
  String _msg='';

  @override
  void initState(){
    super.initState();
    this._getData();
  }

  // 请求数据
  _getData() async{
    // https://jd.itying.com/api/httpGet
    // var apiUrl = Uri.parse('https://jd.itying.com/api/httpGet');
    // var response = await Dio().get('https://jd.itying.com/api/httpGet');
    // print(response.data);
    // print(response.data is Map);
    // setState(() {
    //  this._msg = response.data['msg'];     
    // });

    var apiUrl = 'https://jd.itying.com/api/pcate';
    var response = await Dio().get(apiUrl);
    print(response.data);
    print(response.statusCode);
    if(response.statusCode==200){
      setState(() {
       this._List=response.data["result"];
      });
    } else {
      print(response.statusCode);
    }

  }

  // 提交数据
  _postData() async{
    var apiUrl = 'https://jd.itying.com/api/httpPost';
    var response = await Dio().post(apiUrl, data: {'username':'战三', 'age':'20' });
    print(response.data);
    print(response.data is Map);

    // setState(() {
    //  this._msg = json.decode(response.body)['msg'];     
    // });

  }

  @override
  Widget build(BuildContext context) {
    return 
        // Scaffold(
        //     appBar: AppBar(
        //       title: Text('Demo'),
        //     ),
        //     body: Padding(
        //       padding: EdgeInsets.all(20),
        //       child: Column(
        //         crossAxisAlignment: CrossAxisAlignment.center,
        //         // mainAxisAlignment: MainAxisAlignment.center,
        //         children: <Widget>[
        //           Text(this._msg),
        //           ElevatedButton(
        //             child: Text('get请求数据'),
        //             onPressed: _getData,
        //           ),
        //           SizedBox(height: 20),
        //           ElevatedButton(
        //             child: Text('post请求数据'),
        //             onPressed: _postData,
        //           ),
        //           SizedBox(height: 20),
        //           ElevatedButton(
        //             child: Text('get请求 渲染数据'),
        //             onPressed: (){
        //               Navigator.pushNamed(context, '/http');
        //             },
        //           ),
        //         ],
        //       ),
        //     ),
        // );

        // Scaffold(
        //     appBar: AppBar(
        //       title: Text('请求数据Demo-dio'),
        //     ),
        //     body: this._List.length > 0?ListView.builder(
        //       itemCount: this._List.length,
        //       itemBuilder: (context, index){
        //         return ListTile(title: Text(this._List[index]['title']));
        //       },
        //     ):Text('')
        // );


        Scaffold(
            appBar: AppBar(
              title: Text('请求数据Demo-dio'),
            ),
            body: this._List.length > 0?ListView(
              children: this._List.map((value) {
                return ListTile(title: Text(value['title']));
              }).toList(),
            ):Text('')
        );
    

  }

  
}